
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
		</theme-navbar>
		<!-- #endif -->
		<view class="content u-page">
			<view class="tabsBottom" v-show="tabIndex==0">
				<u-sticky>
					<u-cell-group class="sticky-header">
						<u-cell-item icon="calendar" title="开始/结束日期" :value="rangeDate" :arrow="true"
							@tap="dateRange=true">
						</u-cell-item>
					</u-cell-group>
				</u-sticky>
				<!-- 日计划 上拉加载 -->
				<me-table v-if="type == 'RANDOM_TASK_WARING' || type == 'EMPHASIS_TASK_WARIN'" :i="0" :index="0" :height="getListViewHeight()" :load-data="loadData"
					ref="mescrollItem0" style="padding-top: 50px !important">
					<view slot-scope="d">
						<u-row style="border-bottom: 1px solid #e5e5e5; padding: 5px 0px;" >
							<u-col span="10">
								<client-item :client="d.data"></client-item>
							</u-col>
						</u-row>
					</view>
				</me-table>

				<me-table v-if="type == 'RANDOM_DAY_PLAN_WARING' || type == 'EMPHASIS_DAY_PLAN_WARING' " :i="0" :index="0" :height="getListViewHeight()" :load-data="loadData"
					ref="mescrollItem0" style="padding-top: 50px !important">
					<template #default="d">
						<view class="card">
							<plan-item :plan="d.data"></plan-item>
						</view>
					</template>
				</me-table>
			</view>

		</view>
		
	</view>
</template>

<script>
	import meTable from '@/components/me-table.vue';
	import MescrollList from "@/components/mescroll_list.vue";
	import ClientItem from './componets/client-item.vue';
	import commonService from "@/service/common/common.service.js";
	import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
	import certificateService from '@/service/certificate/certificate.service.js';
	import {
		dateUtils,
	} from "@/common/util"
	import PlanItem from './componets/plan-item.vue';
	import CheckItem from '@/pages/check/components/check-item.vue';
	import {
		CHECK_TASK_SUBJECT,
	} from "@/common/common_enum.js";
	import {
		M_CHECK_GROUP,
		M_TASK_SOURCE,
		M_DAY_PLAN_STATUS,
		M_DAY_CHECK_STATUS,
	} from "@/service/check/mapping/enum.js";
	import fabButton from '@/components/uni-fab.vue';

	export default {
		components: {
			meTable,
			PlanItem,
			CheckItem,
			fabButton,
			ClientItem
		},
		mixins: [MescrollMoreMixin, ],
		data() {
			return {
				type: '',
				day: '',
				dayPlanType: '',
				user: this.$store.state.userInfo,
				M_TASK_SOURCE,
				M_DAY_PLAN_STATUS,
				M_DAY_CHECK_STATUS,
				dateRange: false,
				rangeDate: dateUtils.dateFormat("yyyy-MM-dd", new Date()) + "至" + dateUtils.dateFormat("yyyy-MM-dd", new Date()),
				startDate: dateUtils.dateFormat("yyyy-MM-dd", dateUtils.firstDate(new Date())),
				endDate: dateUtils.dateFormat("yyyy-MM-dd", new Date()),
				nowDay: this.$u.date(Date.now(), 'yyyy-mm-dd'),
				dateChoiceShow: false,
				isOpen: false,
				keyword: "",
				background: {
					background: "#fff",
				},
				backTextStyle: {
					color: "#333333",
				},
				checkList:[]
				
			};
		},
		// 页面加载成功后也要设置一次，用于处理本页面刷新后失效问题
		mounted() {
		},
		onLoad(opt) { 
			this.type = opt.id;
			this.day = opt.day;
			this.dayPlanType = opt.dayPlanType;
			this.startDate = this.getCurrDayAndNDayLater(this.day)[0];
			if(this.type == 'RANDOM_TASK_WARING' || this.type == 'EMPHASIS_TASK_WARIN' ) {
			 this.endDate = this.getCurrDayAndNDayLater(this.day)[1];
			 this.rangeDate = this.getCurrDayAndNDayLater(this.day)[0] + "至" + this.getCurrDayAndNDayLater(this.day)[1];
			this.$refs["mescrollItem0"].refresh();
			}
		},
		
		methods: {
			
			getListViewHeight() {
				let p = (this.$u.sys().windowHeight  - uni.upx2px(300) - this.$u.sys().statusBarHeight) + "px";
				// #ifdef H5-DINGDING || H5-WECHAT
				p = (this.$u.sys().windowHeight - uni.upx2px(300) - this.$u.sys().statusBarHeight) + "px";
				// #endif
				return p;
			},
			getCurrDayAndNDayLater(n) {
				const currentDate = new Date();
				// 计算n天后的日期
				const daysLater = new Date(currentDate);
				daysLater.setDate(currentDate.getDate() + Number(n));
				// 格式化日期为 YYYY-MM-DD
				const formatDate = (date) => {
					const year = date.getFullYear();
					const month = String(date.getMonth() + 1).padStart(2, "0");
					const day = String(date.getDate()).padStart(2, "0");
					return `${year}-${month}-${day}`;
				};
				const currentDay = formatDate(currentDate);
				const nDaysLaterFormattedDate = formatDate(daysLater);
				return [currentDay,nDaysLaterFormattedDate];
			},
			loadData(page) {
				return new Promise((resolve, reject) => {
					if(this.type == 'RANDOM_TASK_WARING' || this.type == 'EMPHASIS_TASK_WARIN') {
						let code = this.type == 'EMPHASIS_TASK_WARIN' ? "01" : "02"
						
						certificateService.findTaskPoolWarningList({
						page: page.num,
						limit: page.size,
						orgUuid: this.curUser.user.org.orgUuid,
						taskStatus: "", //任务状态（字典）
						taskType: code, //任务类型（字典）
						taskSource: "", //任务来源（字典）
						taskSourceItem: "", //任务子来源（字典）
						endDateStart: this.startDate, //任务截止日期开始
						endDateEnd: this.endDate, //任务截止日期结束
						createTimeStart: "", //任务创建日期开始
						createTimeEnd: "", //任务创建日期结束
						menuCode: ""
					}).then(res => {
						if (res.success) {
							resolve({
								count: res.count,
								data: res.data,
							});
						} else {
							this.$u.toast(res.msg);
							resolve({
								count: 0,
								data: [],
							});
						}
					}).catch(err => {
						this.$u.toast(err);
						resolve({
							count: 0,
							data: [],
						});
					});
					} else {
						certificateService.findDayPlanListPage({
						page: page.num,
						limit: page.size,
						orgUuid: this.curUser.user.org.orgUuid,
						dayPlanType: this.dayPlanType, //任务类型（字典）
						planDateStart: this.startDate, //任务截止日期开始
						planDateEnd: this.endDate, //任务截止日期结束
						menuCode: ""
					}).then(res => {
						if (res.success) {
							resolve({
								count: res.count,
								data: res.data,
							});
						} else {
							this.$u.toast(res.msg);
							resolve({
								count: 0,
								data: [],
							});
						}
					}).catch(err => {
						this.$u.toast(err);
						resolve({
							count: 0,
							data: [],
						});
					});
					}
					
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.page {
		padding: 0;
	}

	.content {
		//background-color: #F3F4F5;
		background-color: #FFF;
	}

	.sticky-header {
		position: fixed;
		/* #ifndef H5-ONLY  */
		top: 0px;
		/* #endif */
		/* #ifdef H5-ONLY  */
		top: 45px;
		/* #endif */
		width: 100%;
		z-index: 999;
	}

	.good-popup-lay {
		padding: $cm-content-paddng-row;
		width: 100%;
		height: 100%;

		&__title {
			font-size: 36rpx;
			font-weight: 600;
			color: $u-main-color;
			margin: 10rpx 0;
			text-align: center;
			width: 100%;
		}

		&__btns {
			width: 100%;
			margin-top: 24rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;

			uni-button {
				flex: 0.28;
			}
		}
	}
</style>
